<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class RecordController extends Controller {

    public function index(){
    	$type 	 	= I('get.type');
    	$rootid  	= I('get.rootid');
    	$pici 	 	= I('get.batchnumber');
//     	$awardkind	= I('get.awardkind');
//     	$batch 	 	= A('Batch');
//     	$award_res  = $batch->getrulesarraytoma($batch->getrulestoma($rootid)['isrules']);
//     	$this->assign('rulesarray', $award_res);
		switch ($type){
			//红包记录
			case 3:
				$this->getHbrecord($rootid, $pici);
				break;
			//实物奖品记录
			case 2:
				$this->getJprecord($rootid, $pici);
				break;
			//手机话费充值记录
			case 5:
				$this->getTelphone($rootid, $pici);
				break;
			//手机流量充值记录
			case 4:
				$this->getFlow($rootid, $pici);
				break;
			//非法
			default:
				$this->error('非法参数','',1);
		}
    }
    
    /* 获取手机流量充值记录 */
    protected function getFlow($rootid, $pici){
		session('matitle', null);
    	$telinfo = D(C('TABLE_MOBILEFLOWLOG'));
    	if($pici != '' || $pici != 0) $where['batchnumber'] = $pici;
    	$where['ma_id'] = $rootid;
    	$count = $telinfo->where($where)->count();
    	//每页显示条数
    	$num = 12;
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $telinfo->where($where)->order(array('id'=>'DESC'))->limit(limitbakstr($obj))->select();
    	if($result){
    		$batch = A('Batch');
			$s_matitle = session('matitle');
    		foreach($result as &$v){
    			$ma_randinfo = $v['randnumber'];
				$v['matitle'] = $this->get_matitle($batch, $s_matitle, $ma_randinfo);
				$wxresult = $this->openidTouserinfo($v['useropenid']);
				$v['nickname'] = $wxresult['nickname'];
				$sex = $wxresult['sex'];
				$v['sex'] = $this->check_sex($sex);
    			$v['hjtime'] = $this->randnumberTozjtime($ma_randinfo);
    		}
    	}
    	//消费总额
    	$this->assign('consume_record_total',$this->telphone_record(C('TABLE_MOBILEFLOWLOG'), $rootid, $pici, 'ordercash'));
    	 
    	$this->assign('page',$show);
    	$this->assign('result',$result);
    	$this->display('Record/flowindex');
    }
    
    /* 获取手机话费充值记录 */
    protected function getTelphone($rootid, $pici){
		session('matitle', null);
    	$telinfo = D(C('TABLE_MOBILECHARGELOG'));
    	if($pici != '' || $pici != 0) $where['batchnumber'] = $pici;
    	$where['ma_id'] = $rootid;
    	$count = $telinfo->where($where)->count();
    	//每页显示条数
    	$num = 12;
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $telinfo->where($where)->order(array('id'=>'DESC'))->limit(limitbakstr($obj))->select();
    	if($result){
    		$batch = A('Batch');
			$s_matitle = session('matitle');
    		foreach($result as &$v){
    			$ma_randinfo = $v['randnumber'];
				$v['matitle'] = $this->get_matitle($batch, $s_matitle, $ma_randinfo);
				$wxresult = $this->openidTouserinfo($v['useropenid']);
				$v['nickname'] = $wxresult['nickname'];
				$sex = $wxresult['sex'];
				$v['sex'] = $this->check_sex($sex);
    			$v['hjtime'] = $this->randnumberTozjtime($ma_randinfo);
    		}
    	}
    	//充值总额
    	$this->assign('telphone_record_total',$this->telphone_record(C('TABLE_MOBILECHARGELOG'), $rootid, $pici, 'money'));
    	//消费总额
    	$this->assign('consume_record_total',$this->telphone_record(C('TABLE_MOBILECHARGELOG'), $rootid, $pici, 'ordercash'));
    	
    	$this->assign('page',$show);
    	$this->assign('result',$result);
    	$this->display('Record/telphoneindex');
    }
    
    //消费总额
    protected function telphone_record($table, $rootid, $pici, $field){
    	$telinfo = D($table);
    	if($pici != '' || $pici != 0) $where['batchnumber'] = $pici;
    	$where['ma_id'] = $rootid;
    	$total = $telinfo->where($where)->sum($field);
    	return number_format($total,2);
    	
    }
    
	//获取红包记录
	protected function getHbrecord($rootid, $pici){
		session('matitle', null);
		$randnumberhb = D(C('TABLE_RANDNUMBERHB'));
		if($pici != '' && $pici != 0) $where['batchnumber'] = $pici;
		$where['marootid'] = $rootid;
		$count = $randnumberhb->where($where)->count();
		//每页显示条数
		$num = 12;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $randnumberhb->where($where)->order(array('id'=>'DESC'))->limit(limitbakstr($obj))->select();
		if($result){
			$batch = A('Batch');
			$s_matitle = session('matitle');
			foreach($result as &$v){
				$v['matitle'] = $this->get_matitle($batch, $s_matitle, $v['ma_randnumber']);
				$wxresult = $this->openidTouserinfo($v['ma_user_openid']);
				$v['nickname'] = $wxresult['nickname'];
				$v['sex'] = $this->check_sex($wxresult['sex']);
				$mb_states = $v['ma_money_state'];
				$states_v = array(2, 3, 5, 7);
				if(!in_array($mb_states, $states_v)){
					$hbstatus = $batch->lqstatus($v['ma_mch_billno']);
					$v['hbstatus'] = $hbstatus;
					$this->mchbillnoChangestatus($v['id'], $v['ma_mch_billno'], $hbstatus);
				}else{
					$v['hbstatus'] = $this->intToch($mb_states);
				}
				$v['bgcolor'] = $this->stateTocolor($mb_states);
			}
		}
		$this->assign('page', $show);
		$this->assign('result', $result);
		//总支出
		$this->assign('total', $this->hbTotalexpend($rootid));
		//实际支出

		$pra = $this->practicahbTotalexpend($rootid);
		$prb = number_format($pra, 1);
		$this->assign('practicalTotal', $prb);
		//剩余
		$this->assign('syTotal', number_format(20000-$pra, 1));
		$this->display('Record/hbindex');

	}

	//导出结果到表格
	protected function exportdata($result = null){
		if(!empty($result)){
			exportexcel($result);
		}
	}

	protected function get_matitle($batch, $s_matitle, $randnumber){
		if(!empty($s_matitle)){
			$matitle_res = $s_matitle;
		}else{
			$matitle_res = $batch->randnumbertotitle($randnumber);
			session('matitle', $matitle_res);
		}
		return $matitle_res;
	}

	//判断男女
	protected function check_sex($sex = null){
		if(!empty($sex)){
			switch ($sex){
				case 1:
					$r = '男';
					break;
				case 2:
					$r = '女';
					break;
				default:
					$r = '-';
			}
		}else{
			$r = '-';
		}
		return $r;
	}

	//红包总支出
	protected function hbTotalexpend($rootid){
		$randnumberhb = D(C('TABLE_RANDNUMBERHB'));
		$total = $randnumberhb->where(array('marootid'=>$rootid))->sum('ma_money');
		return number_format($total,1);
	}
	//实际支出
	protected function practicahbTotalexpend($rootid){
		$randnumberhb = D(C('TABLE_RANDNUMBERHB'));
		$total = $randnumberhb->where(array('marootid'=>$rootid,'ma_money_state'=>2))->sum('ma_money');
		return $total;
	}

	//获取实物奖品记录
	protected function getJprecord($rootid, $pici){
		session('matitle', null);
		$telinfo = D(C('TABLE_TELINFO'));
		if($pici != '' && $pici != 0) $where['batchnumber'] = $pici;
		$where['marootid'] = $rootid;
		$count = $telinfo->where($where)->count();
		//每页显示条数
		$num = 12;
		$page = new \Think\Page($count,$num);
		$obj = pagestyle($page);
		$show = $obj->show();
		$result = $telinfo->where($where)->order(array('id'=>'DESC'))->limit(limitbakstr($obj))->select();
		if($result){
			$batch = A('Batch');
			$s_matitle = session('matitle');
			foreach($result as &$v){
				$ma_randinfo = $v['ma_randinfo'];
				$v['matitle'] = $this->get_matitle($batch, $s_matitle, $ma_randinfo);
				$wxresult = $this->openidTouserinfo($v['useropenid']);
				$sex = $wxresult['sex'];
				$v['sex'] = $this->check_sex($sex);
				$v['hjtime'] = $this->randnumberTozjtime($ma_randinfo);
			}
		}
		$this->assign('page',$show);
		$this->assign('result',$result);
		$this->display('Record/shiwuindex');
	}

	//根据用户的微信openid获取用户信息
	protected function openidTouserinfo($useropenid){
		$str = C('APIADDRES1')."/GetUserInfo?token=".$useropenid;
		$result = request_get($str);
		if($result['ret']==1){
			return $result['data'];
		}
	}

	/*
	 * 根据商户单号改变红包领取状态
	 * 1常规  2已领 3已退款 4已发放待领取 5退款中 6发放中 7发放失败
	 */
	protected function mchbillnoChangestatus($id,$mchbillno,$state){
		$randnumberhb = D(C('TABLE_RANDNUMBERHB'));
		$data['ma_money_state'] = $this->chToint($state);
		$res = $randnumberhb->where(array('id'=>$id,'ma_mch_billno'=>$mchbillno))->save($data);
		return $res;
	}
	//根据红包状态（中文）取值（数字）
	protected function chToint($state){
		switch ($state){
			case '已领取':
				return 2;
				break;
			case '已退款':
				return 3;
				break;
			case '已发放待领取':
				return 4;
				break;
			case '退款中':
				return 5;
				break;
			case '发放中':
				return 6;
				break;
			case '发放失败':
				return 7;
				break;
			default:
				return 1;
		}
	}
	//根据红包状态（中文）取值（数字）
	protected function intToch($state){
		switch ($state){
			case 2:
				return '已领取';
				break;
			case 3:
				return '已退款';
				break;
			case 4:
				return '已发放待领取';
				break;
			case 5:
				return '退款中';
				break;
			case 6:
				return '发放中';
				break;
			case 7:
				return '发放失败';
				break;
			default:
				return '-';
		}
	}
	//根据状态显示不同颜色
	protected function stateTocolor($state){
		switch ($state){
			case 2:
				return " style=\"background-color: #009999; color: #fff;\"";
				break;
			case 3:
				return " style=\"background-color: #fb3f51; color: #fff;\"";;
				break;
			case 4:
				return " style=\"background-color: #d9c30d; color: #fff;\"";;
				break;
		}
	}

	//根据randnumber获取中奖时间
	protected function randnumberTozjtime($randn=null){
		$randnumber = D(C('TABLE_RANDNUMBER'));
		$res = $randnumber->field('zjtime')->where(array('ma_randnumber'=>$randn))->find();
		if($res){
			return $res['zjtime'];
		}
	}

}